package com.laboratory.serviceprovide.workBench.service.impl;

import com.Laboratory.workBench.pojo.equipment;
import com.Laboratory.workBench.pojo.equipmentRepair;
import com.Laboratory.workBench.service.equipmentService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.laboratory.serviceprovide.constant.constantArgs;
import com.laboratory.serviceprovide.workBench.mapper.equipmentMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author acok
 * @description:TODO
 * @Date 2022/12/18 20:37
 * @Version 1.0
 */
@DubboService(interfaceClass = equipmentService.class,version = "1.0.0")
public class equipmentServiceImpl implements equipmentService {
    @Resource
    private equipmentMapper equipmentMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public List<equipment> getEquipmentListByPage(Map<String,Object> map) {
        return equipmentMapper.getEquipmentListByPage(map);
    }

    @Override
    public int getEquipmentRowsByPage(String roomNo) {
        return equipmentMapper.getEquipmentRowsByPage(roomNo);
    }

    @Override
    public List<equipmentRepair> getEquipmentRepair() {
        List<equipmentRepair> list=null;
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        if (ops.get(constantArgs.REDIS_REPAIR_LIST_KEY)==null){
            list=equipmentMapper.getEquipmentRepair();
            ops.set(constantArgs.REDIS_REPAIR_LIST_KEY, JSON.toJSONString(list),30*60, TimeUnit.SECONDS);
        }else{
             list = JSON.parseObject(ops.get(constantArgs.REDIS_REPAIR_LIST_KEY), new TypeReference<List<equipmentRepair>>() {});
        }
        //将数据放入缓存
        return list;
    }

    @Override
    public int insertEquipmentRepairRecord(Map<String, Object> map) {
        //获取设备主键
        String id = equipmentMapper.getEquipmentIdByNoAndRoom(
                (String) map.get("equipmentNo"),
                (String) map.get("roomId"),
                (String) map.get("equipmentName"));
        if(id!=null){
            map.put("equipmentId",id);
            //新增报修记录
            int repairRecord = equipmentMapper.insertEquipmentRepairRecord(map);
            //修改设备属性state
            int updateEquState=equipmentMapper.updateEquipmentState((String)map.get("equipmentId"));
            //正常情况下两条sql语句的执行返回值为2

            ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
            if (ops.get(constantArgs.REDIS_REPAIR_LIST_KEY)!=null) {
                ops.set(constantArgs.REDIS_REPAIR_LIST_KEY, JSON.toJSONString(equipmentMapper.getEquipmentRepair()), 30*60, TimeUnit.SECONDS);
            }
            return repairRecord+updateEquState;
        }else {
            return 0;
        }

    }

    @Override
    public int deleteRepairRecordByIds(String[] ids) {
        //将设备状态更新为正常
        int j = equipmentMapper.updateEquipmentStateByIds(ids);
        //将设备报修记录更新
        int i = equipmentMapper.deleteRepairRecordByIds(ids);
        if (i>0 && j>0){
            ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
            if (ops.get(constantArgs.REDIS_REPAIR_LIST_KEY)!=null) {
                ops.set(constantArgs.REDIS_REPAIR_LIST_KEY, JSON.toJSONString(equipmentMapper.getEquipmentRepair()), 30*60, TimeUnit.SECONDS);
            }
            return 1;
        }else {
            return 0;
        }
    }
}
